
	jQuery(document).ready(function(){
		
		jQuery('.sendMessageButton').click(function(){
			clickSendMessageButton();
		});
		
		refreshPage();		
		setInterval(function(){refreshPage();}, 20000);
		
	});
	
	var refreshPage = function(){
		jQuery.ajax({
			url: '../../refreshPage.do',
			data: { },
			type: 'POST',
			dataType: 'json',
			contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
			success: function(data) {				
				
				if (!data) { return; }
				
				if(data.message != '') {
					var valueTextMessage = jQuery('.outText').val() + '\n' + data.message;
					jQuery('.outText').val(valueTextMessage);
				}
				
				jQuery('.userName').text('');
				jQuery('.userNameLabel').text(data.userName);
				jQuery('.userId').val(data.userId);
				jQuery('.userName').val(data.userName);
			}					
		});
	};
	
	var clickSendMessageButton = function(){
		sendMessage();
	};
	
	var sendMessage = function(){
		jQuery.ajax({
			url: '../../send.do',
			data: { message: jQuery('.inputText').val(), userId: jQuery('.userId').val(), userName: jQuery('.userName').val() },
			type: 'POST',
			dataType: 'json',
			contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
			success: function(data) {				
				if (!data) { return; }
			}					
		});
		jQuery('.inputText').val('');
	};
	